//
//  MyTabBar.m
//  df
//
//  Created by scjh on 15/2/25.
//  Copyright (c) 2015年 wangliang0. All rights reserved.
//

#import "MyTabBar.h"

@interface MyTabBar ()

/**
 *  设置之前选中的按钮
 */
@property (nonatomic, weak) UIButton *selectedBtn;
@property(nonatomic,strong)NSArray *itemButtonS;

@end

@implementation MyTabBar

- (void)addButtonWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage {

    UIButton *btn = [[UIButton alloc] init];
    
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    [btn setImage:selectedImage forState:UIControlStateHighlighted];
//    [btn setShowsTouchWhenHighlighted:YES];//点击产生光晕效果
    [btn setAdjustsImageWhenHighlighted:NO];//点击时显示高亮的效果
    [self addSubview:btn];
    
    //带参数的监听方法记得加"冒号"
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchDown];
    
    //如果是第一个按钮, 则选中(按顺序一个个添加)
    if (self.subviews.count == 1) {
        [self clickBtn:btn];
    }
}

/**专门用来布局子视图, 别忘了调用super方法*/
- (void)layoutSubviews {
    [super layoutSubviews];

    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 0.5)];
    lineView.backgroundColor = Color_Line_Gray;
    [self addSubview:lineView];


    NSMutableArray *buttonS = [NSMutableArray array];
    
    int count = (int)self.subviews.count - 2 ;
    for (int i = 0; i < count ; i++) {
        //取得按钮
        UIButton *btn = self.subviews[i];
        
        btn.tag = i; //设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图
        
        CGFloat x = i * self.bounds.size.width / count;
        CGFloat y = 0;
        CGFloat width = self.bounds.size.width / count;
        CGFloat height = self.bounds.size.height;
        btn.frame = CGRectMake(x, y, width, height);
        [buttonS addObject:btn];
    }
    _itemButtonS = buttonS;
    
}

/**
 *  自定义TabBar的按钮点击事件
 */
- (void)clickBtn:(UIButton *)button {
    
    //背景颜色的变化
    self.selectedBtn.backgroundColor = [UIColor clearColor];
    
    //1.先将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    //2.再将当前按钮设置为选中
    button.selected = YES;
    //3.最后把当前按钮赋值为之前选中的按钮
    self.selectedBtn = button;
    
    //却换视图控制器的事情,应该交给controller来做
    //最好这样写, 先判断该代理方法是否实现
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedFrom:to:)]) {
        [self.delegate tabBar:self selectedFrom:self.selectedBtn.tag to:button.tag];
    }
    
    //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)
    //self.selectedIndex = button.tag;
    
   
}

- (void)setIndex:(NSInteger)index {
    
    [self clickBtn:self.itemButtonS[index]];
}

@end
